<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { useAuthStore } from '@/stores/counter'
import { goodStorage } from '@/utils';
import { ref } from "vue";
import { bindGroup } from "./api/user";
const store = useAuthStore()
onLaunch(async () => {
  console.log("App Launch");
  await store.getConfigeInfo()
  const dataInfo = ref<Recordable>({})
  dataInfo.value = getURLParameters(location.href)
  if(dataInfo.value.inviter_code){
    goodStorage.set('refCode',dataInfo.value.inviter_code)
  }
  if(dataInfo.value.code&&dataInfo.value.code!=goodStorage.get('code')){
    await store.login({code:dataInfo.value.code,from_invite_code:goodStorage.get('refCode')})
    goodStorage.set('code',dataInfo.value.code)
  }else{
    goodStorage.set('locationUrl',location.href) 
  }
  if(store.getToken&&store.getToken!=''&&dataInfo.value.inviter_code){
    store.getUserInfo() 
    bindGroup({invite_code:dataInfo.value.inviter_code}).then((res: any)=>{
      console.log(res) 
    })
  }
  // store.login()
  // console.log(getURLParameters(window.location.href))
  if (typeof WeixinJSBridge === 'object' && typeof WeixinJSBridge.invoke === 'function') {
		   handleFontSize()
		} else {
		   if (document.addEventListener) {
			  document.addEventListener('WeixinJSBridgeReady', handleFontSize, false)
		   } else if (document.attachEvent) {
			  document.attachEvent('WeixinJSBridgeReady', handleFontSize)
			  document.attachEvent('onWeixinJSBridgeReady', handleFontSize)
		   }
		}
});
onShow(() => {
  console.log("App Show");
});
onHide(() => {
  console.log("App Hide");
});

function handleFontSize () {
  // 设置网页字体为默认大小
  WeixinJSBridge.invoke('setFontSizeCallback', { fontSize: 0 })
  // 重写设置网页字体大小的事件
  WeixinJSBridge.on('menu:setfont', function () {
    WeixinJSBridge.invoke('setFontSizeCallback', { fontSize: 0 })
  })
}
function getURLParameters(url: string): Record<string, string> {
    const reg = /([^?=&]+)(=([^&#]*))/g;
    const href = url || window.location.href;
    const matchList = href.match(reg) || [];
    const obj = {};
  
    matchList.forEach((v) => {
      obj[v.slice(0, v.indexOf('='))] = decodeURIComponent(v.slice(v.indexOf('=') + 1));
    });
  
    return obj;
  }

</script>
<style lang="scss">
@import '@/static/css/common.css';


</style>
